import {
	getStore,
	setStore
} from '_c/utils/store'
import {
	login,
	logout
} from '_a/login'
import {
	agentInfo,
	myPolicyList,
	agentRole
} from "_a/myInfo";

const user = {
	state: {
		user: getStore(
			'user'
		) || {}, //用户详细信息
		userInfo: getStore(
			'userInfo'
		) || {},
		userAdminInfo: getStore(
			'userAdminInfo'
		) || {},
		policyList: getStore(
			'policyList'
		) || {},
		token: getStore(
			'token'
		) || '',
		tokenMerch: getStore(
			'tokenMerch'
		) || '',
		rememberPasswordUser: getStore(
			'rememberPasswordUser'
		) || null,
		systemConfig: getStore(
			'systemConfig'
		) || {}, //系统配置参数
		isSpecificAccount: getStore(
			'isSpecificAccount'
		) || false, //是否特殊账号
	},
	actions: {
		getUserDetail({
			commit
		}, data) {
			return new Promise((resolve, reject) => {
				agentInfo(data).then((res) => {
					if (res.code === 200 && res.data) {
						commit('SET_USER_INFO', res.data)
						commit('SET_USER', res.data.info)
						myPolicyList().then(res => {
							commit('SET_POLICY_LIST', res.data)

						})
						resolve(res.data)

					}
				}).catch(err => {
					reject(err)
				});
			})
		},
		// 根据用户名登录
		StoreByLogin({
			commit
		}, data) {
			return new Promise((resolve, reject) => {
				login(data).then(res => {
					if (res.code === 200) {
						commit('SET_TOKEN', res.token)
						agentRole().then((res) => {
							if (res.code === 200) {
								commit('SET_USER_ADMIN_INFO', res)
							}
						})
						agentInfo().then((res) => {
							if (res.code === 200 && res.data) {
								commit('SET_USER_INFO', res.data)
								commit('SET_USER', res.data.info)
								resolve(res.data)
							}
						})

						console.log(data)
						/** 如果勾选了记住 则保存 没有勾选 则清空 **/
						if (data.passwordCheck) {
							commit('SET_REMEMBER_PASSWORD', data)
						} else {
							commit('SET_REMEMBER_PASSWORD', null)
						}
					}
					resolve(res)
				}).catch(error => {
					reject(error)
				})
			})
		},
		StoreByLogout({
			commit
		}) {
			return new Promise((resolve, reject) => {
				// logout().then(res => {
				commit('SET_TOKEN', '')
				commit('SET_USER', {})
				commit('SET_USER_INFO', {})
				commit('SET_USER_ADMIN_INFO', {})
				resolve()
				// })
			})
		},

	},
	mutations: {
		SET_POLICY_LIST: (state, detail) => {
			state.policyList = detail
			setStore('policyList', detail)
		},
		SET_USER_INFO: (state, detail) => {
			state.userInfo = detail
			setStore('userInfo', detail)
		},
		SET_USER_ADMIN_INFO: (state, detail) => {
			state.userAdminInfo = detail
			setStore('userAdminInfo', detail)
		},

		SET_TOKEN: (state, token) => {
			state.token = token
			setStore(
				'token',
				token
			)
		},
		SET_TOKEN_MERCH: (state, token) => {
			state.tokenMerch = token
			setStore(
				'tokenMerch',
				token
			)
		},
		SET_USER: (state, userInfo) => {
			state.user = userInfo
			setStore(
				'user',
				userInfo
			)
			console.log(state)
		},
		SET_REMEMBER_PASSWORD: (state, rememberPasswordUser) => {
			/** 记住密码 **/
			state.rememberPasswordUser = rememberPasswordUser
			setStore(
				'rememberPasswordUser',
				rememberPasswordUser
			)
		},
		set_system_config: (state, systemConfig) => {
			state.systemConfig = systemConfig
			setStore(
				'systemConfig',
				systemConfig
			)
		},
		set_isSpecific: (state, value) => {
			state.isSpecificAccount = value
			setStore(
				'isSpecificAccount',
				value
			)
		}
	}
}
export default user
